package com.booking.springrestful.controller;

import com.booking.springrestful.domain.Counselorschedule;
import com.booking.springrestful.domain.Users;
import com.booking.springrestful.domain.request.userBodyRequest;
import com.booking.springrestful.exception.BusinessException;
import com.booking.springrestful.resp.PageResult;
import com.booking.springrestful.resp.ResponseResult;
import com.booking.springrestful.resp.ResultCodeEnum;
import com.booking.springrestful.service.UsersService;
import com.booking.springrestful.utills.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/admin")
@Slf4j
public class adminController {

    @Resource
    private UsersService usersService;
    @PostMapping("/login")
    private ResponseResult userLogin(@RequestBody Users users){
        Users user = usersService.userLogin(users.getUsername(), users.getPassword());
        if (!Objects.equals(user.getUsertype(), "admin")){
            throw new BusinessException(ResultCodeEnum.INTERNAL_SERVER_ERROR,"非管理员账号不可登录");
        }
        String token = JwtUtil.createToken(user);
        return ResponseResult.success(token,"登录成功");

    }

    //按条件查询客户信息
    @PostMapping("/getUserClient")
    private ResponseResult<PageResult<Users>> getUserClient(@RequestBody userBodyRequest userBodyRequest){
        PageResult<Users> userClientVo=usersService.getUserClient(userBodyRequest.getUserDto(),userBodyRequest.getType());
        return ResponseResult.success(userClientVo);
    }


    @PostMapping("/deleteClient")
    private ResponseResult deleteClient(@RequestBody ArrayList<Users> userList){
        if (userList.size()!=0){
            usersService.deleteClient(userList,userList.get(0).getUsertype());
        }
        return ResponseResult.success("删除成功");
    }


    @PostMapping("/updateClient")
    private ResponseResult updateClient(@RequestBody Users users){
        log.info(users.toString());
        usersService.updateClient(users);
        return ResponseResult.success("修改成功");
    }

    @GetMapping("/checkCon")
    private ResponseResult checkCon(){
        Boolean flag = usersService.checkCon();
        return ResponseResult.success(flag);
    }


    //根据userid查工作时间
    @GetMapping("/getWorkTime")
    private ResponseResult<List<Counselorschedule>> getWorkTime(Integer userid){
       List<Counselorschedule> list= usersService.getWorkTime(userid);
       return ResponseResult.success(list);
    }

}
